package com.ruoyi.web.controller.teaching;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.teaching.domain.TeachingCourse;
import com.ruoyi.teaching.domain.TeachingSchool;
import com.ruoyi.teaching.service.ITeachingCourseService;
import com.ruoyi.teaching.service.ITeachingSchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingCoursePrice;
import com.ruoyi.teaching.service.ITeachingCoursePriceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收费标准Controller
 *
 * @author William
 * @date 2022-10-13
 */
@Controller
@RequestMapping("/teaching/price")
public class TeachingCoursePriceController extends BaseController {
    private String prefix = "teaching/price";

    @Autowired
    private ITeachingCoursePriceService teachingCoursePriceService;
    @Autowired
    private ITeachingCourseService teachingCourseService;
    @Autowired
    private ITeachingSchoolService teachingSchoolService;

    //    @RequiresPermissions("teaching:price:view")
    @GetMapping()
    public String price() {
        return prefix + "/price";
    }

    /**
     * 查询收费标准列表
     */
//    @RequiresPermissions("teaching:price:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingCoursePrice teachingCoursePrice) {
        logger.info(teachingCoursePrice.toString());
        startPage();
        List<TeachingCoursePrice> list = teachingCoursePriceService.selectTeachingCoursePriceList(teachingCoursePrice);
        return getDataTable(list);
    }

    /**
     * 导出收费标准列表
     */
    @RequiresPermissions("teaching:price:export")
    @Log(title = "收费标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingCoursePrice teachingCoursePrice) {
        List<TeachingCoursePrice> list = teachingCoursePriceService.selectTeachingCoursePriceList(teachingCoursePrice);
        ExcelUtil<TeachingCoursePrice> util = new ExcelUtil<TeachingCoursePrice>(TeachingCoursePrice.class);
        return util.exportExcel(list, "收费标准数据");
    }

    /**
     * 新增收费标准
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存收费标准
     */
//    @RequiresPermissions("teaching:price:add")
    @Log(title = "收费标准", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingCoursePrice teachingCoursePrice) {
        return toAjax(teachingCoursePriceService.insertTeachingCoursePrice(teachingCoursePrice));
    }

    @PostMapping("/addList")
    @ResponseBody
    public AjaxResult addList(@RequestParam("courseId") Long courseId,
                              @RequestParam("schoolId") Long schoolId,
                              @RequestBody(required = false) List<TeachingCoursePrice> prices) {
        if (StringUtils.isNull(courseId)) {
            return error("请选择课程");
        }
        //判断当前学校是否为0
        if (StringUtils.isNull(schoolId) || schoolId == 0L) {
            return error("请选择开课校区");
        }

        //批量删除该课程该校区下的收费标准
        teachingCoursePriceService.deleteTeachingCoursePriceByCourseIdAndSchoolId(courseId, schoolId);
        int successCount = 0;
        if (StringUtils.isNotNull(prices) && StringUtils.isNotEmpty(prices)) {

            for (TeachingCoursePrice price : prices) {
                if (StringUtils.isNotNull(price.getLessonPrice())
                        && StringUtils.isNotNull(price.getLessonCount())) {
                    price.setCourseId(courseId);
                    price.setSchoolId(schoolId);
                    teachingCoursePriceService.insertTeachingCoursePrice(price);
                    successCount++;
                }
            }
        }
//        return success(String.format("保存成功%s条", successCount));
        return success();
    }

    /**
     * 修改收费标准
     */
//    @RequiresPermissions("teaching:price:edit")
    @GetMapping("/edit/{priceId}")
    public String edit(@PathVariable("priceId") Long priceId, ModelMap mmap) {
        TeachingCoursePrice teachingCoursePrice = teachingCoursePriceService.selectTeachingCoursePriceByPriceId(priceId);
        mmap.put("teachingCoursePrice", teachingCoursePrice);
        return prefix + "/edit";
    }

    /**
     * 修改保存收费标准
     */
//    @RequiresPermissions("teaching:price:edit")
    @Log(title = "收费标准", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingCoursePrice teachingCoursePrice) {
        return toAjax(teachingCoursePriceService.updateTeachingCoursePrice(teachingCoursePrice));
    }

    /**
     * 删除收费标准
     */
//    @RequiresPermissions("teaching:price:remove")
    @Log(title = "收费标准", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teachingCoursePriceService.deleteTeachingCoursePriceByPriceIds(ids));
    }

    @GetMapping("/detail/{courseId}")
    public String detail(@PathVariable("courseId") Long courseId, ModelMap mmap) {
        TeachingCourse teachingCourse = teachingCourseService.selectTeachingCourseByCourseId(courseId);
        mmap.put("course", teachingCourse);
        Long schoolId = getSysUser().getSchoolId();
        TeachingSchool params = new TeachingSchool();
        params.setSchoolId(schoolId);
        List<TeachingSchool> teachingSchools = teachingSchoolService.selectTeachingSchoolList(params);
        mmap.put("schools", teachingSchools);
        return prefix + "/detail";
    }


    /**
     * 三级联动 根据课程id和学校id查询收费标准
     */
    @GetMapping("/select")
    @ResponseBody
    public List<TeachingCoursePrice> select(Long courseId, Long schoolId) {
        List<TeachingCoursePrice> list1 = teachingCoursePriceService.selectTeachingCoursePriceByCourseIdAndSchoolId(courseId, schoolId);
        return list1;
    }
}
